var bimg = ["https://cs-op.douyucdn.cn/dygev/2018/07/19/e8641c2d99f259b0b892277f5469e62b.png", "https://cs-op.douyucdn.cn/dygev/2018/07/19/b45acd6ac21bbc46e406bfdf4d0ae6ef.png", "https://cs-op.douyucdn.cn/dygev/2018/07/19/820b68355b2bdac59fa14618ac998227.png"];

function createLiwuTd(data){
    var td = document.createElement("td");
    if (data.liwuStyle.style > 100){
        td.classList.add("guizu");
        var div = document.createElement("div");
        div.classList.add("noble-banner-item");
        div.classList.add("item-"+(data.liwuStyle.style-100));
        div.style.left=0;
        div.style.position = "relative";
        var div_1 = document.createElement("div");
        div_1.classList.add("item-back")
        var img_1 = document.createElement("img");
        img_1.src = data.liwuStyle.bimg;
        div_1.appendChild(img_1);
        div.appendChild(div_1);
        var div_2 = document.createElement("div");
        div_2.classList.add("item-head");
        div_2.style.borderColor = data.liwuStyle.brgb;
        var img_2 = document.createElement("img");
        img_2.src = data.liwuStyle.himg;
        div_2.appendChild(img_2);
        div.appendChild(div_2);
        var div_3 = document.createElement("div");
        div_3.classList.add("item-name");
        var span_1 = document.createElement("span");
        span_1.classList.add("item-name-inner");
        span_1.appendChild(document.createTextNode(data.auth.name));
        div_3.appendChild(span_1);
        div.appendChild(div_3);
        var div_4 = document.createElement("div");
        div_4.classList.add("item-action-wrap");
        var span_2 = document.createElement("span");
        span_2.classList.add("item-action");
        span_2.appendChild(document.createTextNode("开通"));
        var span_3 = document.createElement("span");
        span_3.classList.add("noble-name");
        span_3.appendChild(document.createTextNode(data.liwuStyle.name));
        div_4.appendChild(span_2);
        div_4.appendChild(span_3);
        div.appendChild(div_4);
        td.appendChild(div);
    } else {
        td.classList.add("liwu");
        var div_p = document.createElement("div");
        div_p.classList.add("gift-banner");
        var img_1 = document.createElement("img");
        img_1.classList.add("gift-banner-bg");
        if (data.liwuStyle.pc < 10000) {
            img_1.src = bimg[0];
        } else if (data.liwuStyle.pc < 50000){
            div_p.classList.add("middle-gift-banner");
            img_1.src = bimg[1];
        } else {
            div_p.classList.add("big-gift-banner");
            img_1.src = bimg[2];
        }
        div_p.appendChild(img_1);


        var div_2 = document.createElement("div");
        div_2.classList.add("gift-banner-info");
        var div_2_h4 = document.createElement("h4");
        // div_2_h4.style.color="#FFF498";
        div_2_h4.appendChild(document.createTextNode(data.auth.name));
        var div_2_p = document.createElement("p");
        // div_2_p.style.color="#FFFFFF";
        var div_2_p_s = document.createElement("span");
        div_2_p_s.classList.add("name")
        // div_2_p_s.style.color="#FFFFFF";
        div_2_p_s.appendChild(document.createTextNode(data.liwuStyle.name));
        div_2_p.appendChild(document.createTextNode("送出"));
        div_2_p.appendChild(div_2_p_s);
        div_2.appendChild(div_2_h4);
        div_2.appendChild(div_2_p);
        div_p.appendChild(div_2);



        var div_3 = document.createElement("div");
        div_3.classList.add("gift-banner-img");
        div_3.style.borderColor=data.liwuStyle.brgb;
        var img_2 = document.createElement("img");
        img_2.src = data.liwuStyle.himg;
        div_3.appendChild(img_2);
        div_p.appendChild(div_3);


        var div_4 = document.createElement("div");
        div_4.classList.add("multiple-num-l");
        div_4.id = "item-count-"+data.id;
        div_4.setAttribute("hits",data.hits);
        var div_4_x = document.createElement("span");
        div_4_x.classList.add("multiple-num-x");
        var div_4_n = newShowHits(data.hits)
        div_4.appendChild(div_4_x);
        div_4.appendChild(div_4_n);
        div_p.appendChild(div_4);


        td.appendChild(div_p);
    }
    return td
}

function createTouxiangTd(data){
    var td = document.createElement("td");
    td.classList.add("td-middle");
    var img = document.createElement("img");
    img.src = "https://apic.douyucdn.cn/upload/"+data.auth.icon+"_small.jpg";
    img.setAttribute("name","icon-"+data.auth.id);
    img.style.width = "40px";
    img.addEventListener("click",function(){showHead(img)});
    td.appendChild(img);
    return td;
}
var guizu_pic={"1":"https://res.douyucdn.cn/resource/2017/02/28/common/65fb47009f8e29ef3f80ebb32ac3ea05.png","2":"https://res.douyucdn.cn/resource/2017/02/28/common/914cdd63edaf2193488d032edca63590.png","3":"https://res.douyucdn.cn/resource/2017/02/28/common/b511553671c24828629f5a7dfdc2c571.png","4":"https://res.douyucdn.cn/resource/2017/02/28/common/f4042fd71ce4a28bd37c8b2e4ad27688.png","5":"https://res.douyucdn.cn/resource/2017/02/28/common/e0b3d8f1eab175dba078a94378e8ef30.png","6":"https://res.douyucdn.cn/resource/2017/02/28/common/d2b17d4f239d866fc14f22eba640ed64.gif","7":"https://res.douyucdn.cn/resource/2017/09/16/common/645ae54da77dd089f4b8c22f81aaccc0.png"};
function createInfoTd(data){
    var name = data.auth.name;
    if (data.auth.nickName != ''){
        name = name + "("+data.auth.nickName+")";
    }
    var td = document.createElement("td");
    td.classList.add("td-middle");
    var div_1 = document.createElement("div");
    div_1.id ="time-"+data.id;
    div_1.appendChild(document.createTextNode(formatDate(data.giveTime)));
    var div_3 = document.createElement("div");
    if (data.auth.authority == 1){
        var span_3_1 = document.createElement("span");
        var img_3_1 = document.createElement("img");
        img_3_1.src = "https://shark.douyucdn.cn/app/douyu/res/page/room-normal/roomadmin.png?v=20170706";
        span_3_1.appendChild(img_3_1);
        div_3.appendChild(span_3_1);
    }
    if (data.auth.guizu){
        if (data.auth.guizu > 0){
            var span_3_2 = document.createElement("span");
            var img_3_2 = document.createElement("img");
            img_3_2.src = guizu_pic[data.auth.guizu];
            span_3_2.appendChild(img_3_2);
            div_3.appendChild(span_3_2);
        }
    }
    var a_3_3 = document.createElement("a");
    a_3_3.classList.add("level");
    a_3_3.classList.add("level-"+data.auth.lv);
    div_3.appendChild(a_3_3);
    var div_2 = document.createElement("div");
    div_2.setAttribute("name","name-"+data.auth.id);
    div_2.appendChild(document.createTextNode(name));
    td.appendChild(div_1);
    td.appendChild(div_3);
    td.appendChild(div_2);
    if (data.auth.oldName){
        var div_3 = document.createElement("div");
        div_3.setAttribute("name","oldName-"+data.auth.id);
        div_3.appendChild(document.createTextNode("【旧】"+data.auth.oldName));
        td.appendChild(div_3);
    }
    return td;
}

function createXTd(data){
    var td = document.createElement("td");
    td.classList.add("td-top");
    var div = document.createElement("div");
    div.classList.add("glyphicon");
    div.classList.add("glyphicon-remove");
    div.classList.add("Xdiv");
    div.addEventListener("click",function(){closeOne(data.id)});
    td.appendChild(div);
    return td;
}

function createChatmsgTd(data){
    var td = document.createElement("td");
    td.classList.add("td-middle");
    var a = document.createElement("a");
    a.classList.add("btn");
    a.classList.add("btn-info");
    a.classList.add("btn-sm");
    a.addEventListener("click",function(){getAuthTalk(data.auth.id)});
    a.appendChild(document.createTextNode("弹"));
    var a2 = document.createElement("a");
    a2.classList.add("btn");
    a2.classList.add("btn-primary");
    a2.classList.add("btn-sm");
    var span = document.createElement("span");
    span.classList.add("glyphicon");
    span.classList.add("glyphicon-volume-up");
    a2.appendChild(span);
    a2.addEventListener("click",function(){playAudioLiwu(data.auth.name,data.liwuStyle.name)});
    var a3 = document.createElement("a");
    a3.classList.add("btn");
    a3.classList.add("btn-success");
    a3.classList.add("btn-sm");
    a3.appendChild(document.createTextNode("详"));
    a3.addEventListener("click",function(){getAuthDetail(data.auth.id)});

    td.appendChild(a);
    td.appendChild(a3);
    td.appendChild(document.createElement("br"));
    td.appendChild(a2);
    return td;
}

function createUserChatmsgTd(data){
    var td = document.createElement("td");
    td.classList.add("td-middle");
    var div = document.createElement("div");
    div.classList.add("div-chatmsg")
    div.setAttribute("name","td-chatmsg-"+data.auth.id);
    td.appendChild(div);
    return td;
}

function showTdChatmsg(id,txt){
    var tds = document.getElementsByName("td-chatmsg-"+id);
    for(var i=0;i<tds.length;i++){
        tds[i].innerHTML = txt;
    }
}

function addTr(data){
    var liwuTable = document.getElementById("liwuTable");
    var tr = document.getElementById(data.id)
    if (tr){
        var itemCount = document.getElementById("item-count-"+data.id);
        if (itemCount && itemCount.attributes["hits"].nodeValue != data.hits){
            itemCount.hits = data.hits;
            itemCount.replaceChild(newShowHits(data.hits),itemCount.lastChild);
            var name = data.auth.name;
            if (data.auth.nickName != ''){
                name = name + "("+data.auth.nickName+")";
            }
            var itemNames = document.getElementsByName("item-name-"+data.auth.id);
            var names = document.getElementsByName("name-"+data.auth.id);
            var oldNames = document.getElementsByName("oldName-"+data.auth.id);
            var icons = document.getElementsByName("icon-"+data.auth.id);
            for (var i = 0;i < itemNames.length;i++){
                itemNames[i].innerHTML=data.auth.name;
                names[i].innerHTML=name;
                if(oldNames[i]){
                    oldNames[i].innerHTML="【旧】"+data.auth.oldName;
                }
                icons[i].src = "https://apic.douyucdn.cn/upload/"+data.auth.icon+"_small.jpg";
            }
            document.getElementById("time-"+data.id).innerHTML = formatDate(data.giveTime);
            liwuTable.insertBefore(document.getElementById(data.id),liwuTable.firstChild);
        } else {
            forFlag = false;
        }
    } else {
        tr = document.createElement("tr");
        tr.classList.add("liwu-style-"+data.liwuStyle.style);
        tr.classList.add("liwu-style");
        tr.setAttribute("liwu-style-id",data.liwuStyle.gfid);
        tr.id = data.id;
        tr.appendChild(createLiwuTd(data));
        tr.appendChild(createTouxiangTd(data));
        tr.appendChild(createInfoTd(data));
        tr.appendChild(createXTd(data));
        tr.appendChild(createChatmsgTd(data));
        tr.appendChild(createUserChatmsgTd(data));
        liwuTable.insertBefore(tr,liwuTable.firstChild);
    }
    if(hideLiwuStyleIds){
        if(hideLiwuStyleIds.length > 1 && tr.style.display != 'none'){
            for(var i=1;i<hideLiwuStyleIds.length;i++){
                if (hideLiwuStyleIds[i] == tr.getAttribute("liwu-style-id")){
                    tr.style.display = "none";
                    return;
                }
            }
        }
    }

}
function showHead(data){
    window.open($(data).attr("src").replace("_small.jpg","_big.jpg"));
}

function closeOne(id){
    var json = {"ids":[id]};
    closeLiwu(json);
    $("#"+id).remove();
}

function closeAll(){
    var trs = $("#liwuTable").find("tr");
    var json = {"ids":[]};
    for(i=0;i<trs.length;i++){
        var id = $(trs[i]).attr("id");
        if (id != 0){
            json.ids[json.ids.length]=id;
        }
    }
    closeLiwu(json);
    location = location;
}

function showHits(hits){
    var div = document.createElement("div");
    div.classList.add("js-hittimes");
    div.classList.add("nbox");
    while (hits >= 1){
        var n = hits % 10;
        var span = document.createElement("span");
        span.classList.add("n");
        span.classList.add("n"+n);
        div.insertBefore(span,div.firstChild);
        hits = parseInt(hits / 10);
    }
    return div;
}

function newShowHits(hits){
    var div = document.createElement("div");
    div.classList.add("multiple-nums");
    while (hits >= 1){
        var n = hits % 10;
        var span = document.createElement("span");
        span.classList.add("multiple-num-"+n);
        div.insertBefore(span,div.firstChild);
        hits = parseInt(hits / 10);
    }
    return div;
}
